# -*- coding: utf-8 -*-
"""
Created on Sat Apr  9 21:07:07 2022

官方SDK文档： https://developer.qiniu.com/kodo/1242/python

@author: singularity
"""

import os
import sys
import queue
from qiniu import Auth, put_file

# 本地需要同步上去的路径
localImgFile_RootPath = './'

# 个人中心->密匙管理->AK
access_key = 'waANxRrmWw4Bd0CsTOPwa_drsDirljlu35XATwck'
# 个人中心->密匙管理->SK
secret_key = 'GpVdlCr--wX7CZ4zSEbZnC6BJmOS0TDtveIzIMN1'
# 七牛空间名
bucket_name = 'singularity-blog'


'''-- 七牛 --'''
#构建鉴权对象
qiniu_auth = Auth(access_key, secret_key)

# =============================================================================
# # 上传后保存的文件名
# key = "./20220409/cnm2.png"
# # 生成上传Token，可以指定过期时间
# token = qiniu_auth.upload_token(bucket_name,
#                                 key,
#                                 3600)
# localfile = './20201011/20201011-intel-1.png'
# 
# ret,info = put_file(token,key,localfile,version='v2')
# print(info)
# =============================================================================



'''-- 广度优先 遍历文件 --'''
target_path_queue = queue.Queue()

for name in os.listdir(localImgFile_RootPath):  # 根目录下的文件不上传
    if os.path.isdir(name):
        target_path_queue.put(name+'/')

while not target_path_queue.empty():    # 只要队列里有东西，就继续搜索
    pathNow = target_path_queue.get();
    
    file_and_dir_in_pathNow = os.listdir(pathNow)
    
    for name in file_and_dir_in_pathNow:
        if os.path.isdir(name):
            name = name +'/'
            print("directory:"+pathNow + name)
            target_path_queue.put(name)
            
        else:
            print("file:" + pathNow + name)
            
            
            localfile = pathNow + name
            key = localfile
            token = qiniu_auth.upload_token(bucket_name,
                                            key,
                                            10)
            ret,info = put_file(token,
                                key,
                                localfile,
                                version='v2')
            assert ret['key'] == key
            
            
            
    

